package org.mock.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson2.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.mock.entity.SabreOrderBooking;
import org.mock.enums.SabreMockStateEnum;
import org.mock.mapper.SabreOrderBookingMapper;
import org.mock.service.SabreMockSceneService;
import org.mock.service.SabreOrderBookingService;
import org.mock.dto.SabreOrderBookingDTO;
import org.mock.common.Condition;
import org.mock.utils.BeanCopyUtils;
import org.springframework.stereotype.Service;
import lombok.AllArgsConstructor;
import cn.hutool.core.util.StrUtil;

import java.util.List;

/**
 * sabre_order_booking 服务实现类
 *
 * @author AI
 * @since 2025-06-13
 */
@Service
@AllArgsConstructor
@Slf4j
public class SabreOrderBookingServiceImpl implements SabreOrderBookingService {

    protected SabreOrderBookingMapper sabreOrderBookingMapper;
    private SabreMockSceneService sabreMockSceneService;

    @Override
    public SabreOrderBookingDTO handleRequest(JSONObject request) {
        log.info("[getBooking] 请求：{}", request);
        String confirmationId = request.get("confirmationId").toString();

        SabreOrderBookingDTO dto = new SabreOrderBookingDTO();
        dto.setConfirmationId(confirmationId);

        return dto;
    }

    @Override
    public SabreOrderBookingDTO handleQueryWrapper(SabreOrderBookingDTO dto) {
        String currentMockNo = sabreMockSceneService.getMockNoByStatus(SabreMockStateEnum.ON_REQUEST.getValue());
        if ("000".equals(currentMockNo)) {
            log.info("当前查询模式：忽略所有的查询条件，直接给一个新的dto");
            dto = new SabreOrderBookingDTO();
        }

        return dto;
    }

    @Override
    public String handleData(SabreOrderBooking data) {
        if (ObjectUtil.isEmpty(data)) {
            log.info("暂无数据");
            return StrUtil.EMPTY;
        }
        return data.getResponse();
    }

    @Override
    public SabreOrderBooking getOne(SabreOrderBookingDTO dto) {
        List<SabreOrderBooking> list = sabreOrderBookingMapper.selectList(Condition.getQueryWrapper(BeanCopyUtils.copy(dto, SabreOrderBooking.class)));
        if (CollUtil.isEmpty(list)) {
            return null;
        } else {
            return list.get(0);
        }
//        return sabreOrderBookingMapper.selectOne(Condition.getQueryWrapper(BeanCopyUtils.copy(dto,SabreOrderBooking.class)));
    }
}
